<div class="page-group">
	<div class="page" data-page="book_kind">
		<header class="bar bar-nav">
			<a href="{:U('User/index')}" class="icon" style="width: 40%"> <img
				src="__PUBLIC__/img/iconfont_back@2x.png" class="back_img">
			</a>
			<span>购书记录</span>
		</header>
		<div class="content">
			<div class="buttons-tab">
				<a onclick="showorder(this,-1)" class="tab-link button active" id="order_btn_">全部</a>
				<a onclick="showorder(this,0)" class="tab-link button" id="order_btn_#0">待付款</a>
				<a onclick="showorder(this,1)" class="tab-link button" id="order_btn_#1">待发货</a>
				<a onclick="showorder(this,2)" class="tab-link button" id="order_btn_#2">待收货</a>
				<a onclick="showorder(this,3)" class="tab-link button" id="order_btn_#3">待评价</a>
			</div>
			<div class="list-block media-list" style="margin:0;">
				<ul id="order_num" style="background:none;">
					<volist name="d" id="v">
							<!----	0-待付款	-未付款---->
							<!--	1-待发货	-	已付款未发货-	-->
							<!--	2-待收货	-	已发货，配送中	-->
							<!--	3-待评价	-	已收货	-->
							<!--	4-交易成功	-	完成评价-	-->
						<li class="order-item order-item-{$v.state_order}">
							<div style="background: #FFF;padding: 5px; vertical-align: baseline;; height: 40px">
								<img src="__PUBLIC__/img/bookstore@2x.png" style="display: block; float: left">
								<span style="line-height: 30px; float: left; font-size: 14px;font-weight:bold;">
									新华书店
									&nbsp;&nbsp;订单号:{$v.order_id}
								</span>

								<span class="order-item-state">
									<switch name="v.state_order">
										<case value="0">待付款</case>
										<case value="1">待发货</case>
										<case value="2">待收货</case>
										<case value="3">交易成功,待评价</case>
										<case value="4">交易成功</case>
										<default />
									</switch>
								</span>
							</div>
							<a class="item-link item-content" href="{:U("User/buy_detail","id=".$v['order_id'])}" style="padding-left: 1rem; background: #F5F5F5">

								<div class="item-media">
									<img src="{$v.order_has_book.0.b_logo}" style='width: 4rem;'>
								</div>
								<div class="item-inner">
									<div class="item-title-row">
										<div class="item-title">《{$v.order_has_book.0.sm}》</div>
									</div>
									<div class="item-subtitle color_price">￥{$v.order_has_book.0.dj|number_format=###,2}</div>
									<div class="item-text" style="height: auto">X{$v.order_has_book.0.quantity}</div>
									<!--div class="item-text" style="height: auto;"><input style="background:#DC2222 ;width: auto;border:none;color:#FFF" type="button" value="七天退货"></div-->
								</div>
							</a>
							<!--div class="trans_fee">
								<span>配送方式</span> <span class="user_info_public">快递7.00元</span>
							</div-->
							<div class="trans_fee">
								<span class="user_info_public order_rent_price">
									共<span id="total_num">{$v.total_rent}</span>件商品
									<s style="color:red;">合计：￥{$v['money']+$v['price_freight']|number_format=###,2}元（含运费：￥{$v.price_freight|number_format=###,2}元）</s>
									<br>
									折后：￥{$v['money']*$v['discount_buy']+$v['price_freight']|number_format=###,2}元（含运费：￥{$v.price_freight|number_format=###,2}元）
								</span>
							</div>
							<div class="trans_fee" style="text-align: right;">
								<span style="float:left;font-size:0.6rem;">创建时间:{$v.create_time}</span>
								<switch name="v.state_order">
									<case value="0">
										<input value="取消订单" type="button" onclick="cancel_order('{:U("Order/cancelorder","id=".$v['pay_id'])}')" class="order_op_button color_opbac_button">
										<input value="付款" type="button" onclick="confirm_pay({$v['pay_id']})" class="order_op_button color_opbac_button_com">
									</case>
									<case value="1">
										<input value="待发货" type="button" class="order_op_button color_opbac_button_com">
									</case>
									<case value="2">
										<input value="查看物流" onclick="window.location.href='{:U("Order/express","id=".$v['order_id'])}'" type="button" class="order_op_button color_opbac_button">
										<input value="确认收货" onclick="confirm_receive('{:U("Order/confirmreceive","id=".$v['order_id'])}')" type="button" class="order_op_button color_opbac_button_com">
									</case>
									<case value="3">
										<input value="评价" onclick="window.location.href='{:U("Comment/write","id=".$v['order_id'])}'" type="button" class="order_op_button color_opbac_button_com">
									</case>
									<case value="4">
										<input value="已评价" type="button" class="order_op_button color_opbac_button_com">
									</case>
									<case value="5">
										<input value="已退款" type="button" class="order_op_button color_opbac_button_com">
									</case>
									<default />
								</switch>

							</div>
						</li>

					</volist>
				</ul>
			</div>
		</div>
	</div>
</div>
<script>
	autoshow();
	function autoshow(){
		console.debug(window.location.hash);
		document.getElementById("order_btn_"+window.location.hash).click();
	}

	function showorder($this,$state){
		var tabbutton=document.getElementsByClassName("tab-link button");
		for(var i=0;i<tabbutton.length;i++){
			tabbutton[i].setAttribute("Class","tab-link button");
		}
		$this.setAttribute("Class","tab-link button active ");
		var orderitem=document.getElementsByClassName("order-item");
		for(var i=0;i<orderitem.length;i++){
			var cname="order-item order-item-"+$state;
			if($state==-1 || orderitem[i].className==cname){
				orderitem[i].style.display="block";
			}else{
				orderitem[i].style.display="none";}
		}
	}

	function confirm_pay($pay_id){
		if(confirm('确认付款?')){
			//window.location.href='{:U("Pay/pay_rent","id=".$v['pay_id'])}';
			Unifiedorder($pay_id);
		}
	}
	function confirm_receive($url){
		if(confirm('确认收货?')){
			window.location.href=$url;
		}
	}

	function cancel_order($url){
		if(confirm('确认取消?')){
			window.location.href=$url;
		}
	}

	//统一下单
	function Unifiedorder($pay_id){

		$.post("{:U('Order/getJsApiParameters_buy')}",{id:$pay_id},function(data){
			var data= eval('(' + data + ')');

			console.log(data);
//			return
			if(data.status=='complete'){
				alert(data.message);
				window.location.reload();
			}
			if(data.status=="error"){
				alert("无法支付，"+data.message);
			}
			if(data.status=="success"){
				callpay(data.message);
			}
		});
	}


	//微信支付
	function callpay(data)
	{
		if (typeof WeixinJSBridge == "undefined"){
			if( document.addEventListener ){
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			}else if (document.attachEvent){
				document.attachEvent('WeixinJSBridgeReady', jsApiCall);
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			}
		}else{
			jsApiCall(data);
		}
	}

	function jsApiCall($jsApiParameters)
	{
		WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				eval('(' + $jsApiParameters + ')'),
//				$jsApiParameters,
				function(res){
					WeixinJSBridge.log(res.err_msg);
//					alert(res.err_code+res.err_desc+res.err_msg);
//					return;
					window.location.reload();
				}
		);
	}
</script>